package com.idrem.views.visit;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import java.util.HashMap;

public class ImageListView extends ListView implements
		AbsListView.OnScrollListener, AdapterView.OnItemSelectedListener,
		ImageListViewAdapter.ImageListViewFooterChannel, View.OnTouchListener {

	private ImageListViewAdapter mAdapter;
	private int mFirstVisibleItem;
	private View mFooterView;
	private int mTotalItemCount;
	private int mVisibleItemCount;
	private int scrollState;

	public ImageListView(Context paramContext) {
		super(paramContext);
		init(paramContext);
	}

	public ImageListView(Context paramContext, AttributeSet paramAttributeSet) {
		super(paramContext, paramAttributeSet);
		init(paramContext);
	}

	public ImageListView(Context paramContext, AttributeSet paramAttributeSet,
			int paramInt) {
		super(paramContext, paramAttributeSet, paramInt);
		init(paramContext);
	}

	private void init(Context paramContext) {
		setOnScrollListener(this);
		setOnItemSelectedListener(this);
		setVerticalFadingEdgeEnabled(false);
		setTranscriptMode(AbsListView.TRANSCRIPT_MODE_NORMAL);
	}

	private boolean isScrollToBottom() {
		if (this.mFirstVisibleItem + this.mVisibleItemCount == getCount())
			return true;
		else
			return false;
	}

	public void addFooterView(View paramView) {
		super.addFooterView(paramView);
		this.mFooterView = paramView;
	}

	public void hideFooterView() {
		if ((this.mFooterView == null) || (getFooterViewsCount() <= 0))
			return;
		removeFooterView(this.mFooterView);
	}

	public void onItemSelected(AdapterView<?> paramAdapterView, View paramView,
			int paramInt, long paramLong) {
		ImageListViewAdapter localImageListViewAdapter = this.mAdapter;
		if (paramInt != -1 + getCount())
			return;
		localImageListViewAdapter.startLoadMore(this.mFirstVisibleItem,
				this.mVisibleItemCount, this.mTotalItemCount);
	}

	public void onNothingSelected(AdapterView<?> paramAdapterView) {
	}

	public void onScroll(AbsListView paramAbsListView, int paramInt1,
			int paramInt2, int paramInt3) {
		this.mFirstVisibleItem = paramInt1;
		this.mVisibleItemCount = paramInt2;
		this.mTotalItemCount = paramInt3;
	}

	public void onScrollStateChanged(AbsListView paramAbsListView, int paramInt) {
		this.scrollState = paramInt;
		ImageListViewAdapter localImageListViewAdapter = this.mAdapter;
		if (localImageListViewAdapter != null) {
			if ((2 != paramInt) && (1 != paramInt)) {
				localImageListViewAdapter.enableSmoothMode(true);
				localImageListViewAdapter.clearTaskQueue();
				localImageListViewAdapter.stopImageLoadTask();
			} else {
				localImageListViewAdapter.enableSmoothMode(false);
				localImageListViewAdapter.initLoadQueue(this.mFirstVisibleItem,
						this.mVisibleItemCount);
				localImageListViewAdapter.startImageLoadTask();
			}
		}
		if (!isScrollToBottom())
			localImageListViewAdapter.startLoadMore(this.mFirstVisibleItem,
					this.mVisibleItemCount, this.mTotalItemCount);
	}

	public boolean onTouch(View paramView, MotionEvent paramMotionEvent) {
		if ((paramMotionEvent.getAction() == 2) && (this.scrollState != 2)) {
			refreshAdapter();
			return true;
		} else
			return false;
	}

	public void refreshAdapter() {
		ImageListViewAdapter localImageListViewAdapter = this.mAdapter;
		if (localImageListViewAdapter == null)
			localImageListViewAdapter.imageCache.clear();
		localImageListViewAdapter.enableSmoothMode(false);
		localImageListViewAdapter.initLoadQueue(this.mFirstVisibleItem,
				this.mVisibleItemCount);
		localImageListViewAdapter.startImageLoadTask();
	}

	public void setAdapter(ListAdapter paramListAdapter) {
		if (paramListAdapter == null)
			throw new IllegalArgumentException("Adapter can not be null");
		if (paramListAdapter instanceof ImageListViewAdapter) {
//			super.setAdapter(paramListAdapter);
			this.mAdapter = ((ImageListViewAdapter) paramListAdapter);
			this.mAdapter.setFooterChannel(this);
			this.mAdapter.initLoadQueue(0, 10);
			this.mAdapter.startImageLoadTask();
			hideFooterView();
		}
		throw new IllegalArgumentException(
				"This adapter must be the subClass of ImageListViewAdapter");
	}

	public void showFooterView() {
		if ((this.mFooterView == null) || (getFooterViewsCount() != 0))
			return;
		addFooterView(this.mFooterView, null, true);
		setSelection(-1 + getCount());
	}
}